<?php
/**
 * Created by PhpStorm.
 * User: Eric
 * Date: 16/5/30
 * Time: PM3:00
 */

System::load_app_class('base','mobileapi','no');

class commodity extends base{
    public function __construct() {
        parent::__construct();
        $this->db=System::load_sys_class('model');
    }
    public function unserializeArray($array){
        foreach ($array as $key => $value) {
            if(array_key_exists('info',$value))			{$array[$key]['info']=unserialize($value['info']);}
            if(array_key_exists('q_user',$value))		{$array[$key]['q_user']=unserialize($value['q_user']);}
            if(array_key_exists('picarr',$value))		{$array[$key]['picarr']=unserialize($value['picarr']);}
            if(array_key_exists('q_content',$value))	{$array[$key]['q_content']=unserialize($value['q_content']);}
        }
        return $array;	
    }
    //商品详细
    public function item(){
        $itemid=safe_replace($_POST["id"]);
        $shopsid = safe_replace($_POST["shopsid"]);
        if($itemid){
            $item=$this->db->GetOne("select `id`,`sid`,`cateid`,`title`,`title2`,`shenyurenshu`,`zongrenshu`,`canyurenshu`,`qishu`,`q_uid`,`q_user`,`q_user_code`,`q_counttime`,`q_end_time`,`thumb`,`q_content`,`money`,`picarr`,`content`  from `@#_shoplist` where `id`='".$itemid."'LIMIT 1");
        }else if($shopsid){
            $item = $this->db->GetOne("select `id`,`sid`,`cateid`,`title`,`title2`,`shenyurenshu`,`zongrenshu`,`canyurenshu`,`qishu`,`q_uid`,`q_user`,`q_user_code`,`q_counttime`,`q_end_time`,`thumb`,`q_content`,`money`,`picarr`,`content`  from `@#_shoplist` where `sid`='".$shopsid."'  ORDER BY `id` DESC LIMIT 1");
            $itemid = $item['id'];
        }
        if(!$item)$this->failed("商品不存在！或已下架");
         $item['picarr'] = unserialize($item['picarr']);
        $item['q_content'] =$item['q_content'] ? unserialize($item['q_content']): null;
        $info=array("item"=>$item,"records"=>$this->getRecordsArray(1,$itemid));
        $this->success($info);
    }
    public function getRecordsArray($p=1,$itemid) {
        $end=20;
        $star=($p-1)*$end;
        $count = $this->db->GetList("select * from `@#_member_go_record` where `shopid`='$itemid' and `goucode` != '' ");
        $pagex=floor(count($count)/$end);
        $isNoData = false;
        if($pagex == $p || count($count)<=1){
            $isNoData = ture;
        }
        $cords=$this->db->GetList("select `uid`,`username`,`uphoto`,`gonumber`,`ip`,`time` from `@#_member_go_record` where `shopid`='$itemid' and `goucode` != '' order by `id` desc limit $star,$end");
        if(!$cords){
            return array("isNoData"=>true,"records"=>null);
        }
        else{
            return array("isNoData" => $isNoData,"info" => $cords);
        }
    }



    public  function buyrecords() {
        $p = $_POST['page'] ? $_POST['page'] : 1;
        $itemid=$_POST['id'];
        $info = $this->getRecordsArray($p,$itemid);
        $this->success($info);
    }

    public function search(){
            $keyword = $_POST['keyword'];
            $shopid = $_POST['businessid'];
            $sid = $_POST['sid'];
//        	$this->success($keyword);
            if($keyword){
                $total = $this->db->GetCount("SELECT `id` FROM `@#_shoplist` WHERE concat(`title`,`title2`,`keywords`) like '%$keyword%' AND `q_uid` is null  ");
               
                $num = 10;
                $page = System::load_sys_class('page');
                if (isset($_POST['page'])) {
                    $pagenum = $_POST['page'];
                } else {
                    $pagenum = 1;
                }
                $page->config($total, $num, $pagenum, "0");
                if ($pagenum > $page->page) {
                    $pagenum = $page->page;
                }

                $result = $this->db->GetPage("SELECT `id`,`sid`,`cateid`,`title`,`shenyurenshu`,`zongrenshu`,`canyurenshu`,`thumb`  FROM `@#_shoplist` WHERE concat(`title`,`title2`,`keywords`) like '%$keyword%'AND `q_uid` is null  ORDER BY `qishu` DESC",array("num" => $num, "page" => $pagenum, "type" => 1, "cache" => 0));
               
                $yushu = $total % $num;
                $yeshu = floor($total / $num) + 1;
                $isNoData = false;
                if ($yushu > 0 && $yeshu == $pagenum) {
                    $isNoData = true;
                }

                if($result){
                    $result = $this->unserializeArray($result);
                    $this->success(array("isNoData" => $isNoData,"info"=>$result));
                }else{
                    $this->failed("无相关产品或商家");
                }
            }else if($shopid || $shopid==0){
                $total = $this->db->GetCount("SELECT `id` FROM `@#_shoplist` WHERE `businessid` = $shopid and `zongrenshu`>`canyurenshu` ");
                $num = 10;
                $page = System::load_sys_class('page');
                if (isset($_POST['page'])) {
                    $pagenum = $_POST['page'];
                } else {
                    $pagenum = 1;
                }
                $page->config($total, $num, $pagenum, "0");
                if ($pagenum > $page->page) {
                    $pagenum = $page->page;
                }
                $result = $this->db->GetPage("SELECT `id`,`sid`,`cateid`,`title`,`shenyurenshu`,`zongrenshu`,`canyurenshu`,`thumb`   FROM `@#_shoplist` WHERE `businessid`=$shopid and `zongrenshu`>`canyurenshu`  ORDER BY `id`",array("num" => $num, "page" => $pagenum, "type" => 1, "cache" => 0));
                $yushu = $total % $num;
                $yeshu = floor($total / $num) + 1;
                $isNoData = false;
                if ($yushu > 0 && $yeshu == $pagenum) {
                    $isNoData = true;
                }
                if($result){
                    $result = $this->unserializeArray($result);
                    $this->success(array("isNoData" => $isNoData,"info"=>$result));
                }else{
                    $this->failed("无相关商家");
                }
            }else if($sid){
                
                $result = $this->db->GetOne("SELECT `id`,`sid`,`cateid`,`title`,`title2`,`shenyurenshu`,`zongrenshu`,`canyurenshu`,`qishu`,`q_uid`,`q_user`,`q_user_code`,`q_counttime`,`q_end_time`,`thumb`,`q_content`,`money`,`picarr`,`content`  FROM `@#_shoplist` WHERE `sid` = $sid and `canyurenshu`<`zongrenshu`  ORDER BY `id` DESC  limit 1");
    //			var_dump("SELECT * FROM `@#_shoplist` WHERE `sid` = $sid and `canyurenshu`<`zongrenshu` ORDER BY `id` DESC  limit 1");
                if($result){
                    $this->success(array("isNoData"=>true,'info'=>array($result)));
                }else{
                    $this->failed("产品不存在或已下架");
                }
            }

            else{
                $this->failed("无相关产品或商家");
            }
        }

}